package com.example.juhedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "驾照题库";
    private static final String SUCCESS = "请求成功";
    private static final String FAILURE = "请求失败";
    private static final String BASE_URL = "http://v.juhe.cn/jztk/";
    private static final String KEY = "f8cf03fbe62c68e313fb01af2121610a";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        query();
    }

    private void query() {
        // 创建 Retrofit 对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        // 创建 网络请求接口 的实例
        final QuestionService request = retrofit.create(QuestionService.class);
        // 对发送请求进行封装
        Call<QuestionBean> call = request.getResult("1", "c1", "rand", KEY);
        // 发送请求(异步)
        call.enqueue(new Callback<QuestionBean>() {
            @Override
            public void onResponse(Call<QuestionBean> call, Response<QuestionBean> response) {
                Log.d(TAG, SUCCESS);
                QuestionBean bean = response.body(); // response.body()是个QuestionBean
                Log.d(TAG, "请求到的数据对象: " + bean); // 打印获取到的请求的数据
                // QuestionBean转Json
                Gson gson = new Gson();
                String data = null;
                data = gson.toJson(bean);
                Log.d(TAG, "获取的JSON数组: " + data);
                // 解析JSON数组
                try {
                    // 创建JSON对象实例
                    JSONObject jsonObjectAll = new JSONObject(data);
                    // 解析对象中的内容
                    int errorCode = jsonObjectAll.optInt("error_code", 0);
                    String reason = jsonObjectAll.optString("reason", null);
                    Log.d(TAG, "error_code: " + errorCode + "\n" + "reason: " + reason);
                    // 创建JSON数组实例
                    JSONArray jsonArray = jsonObjectAll.optJSONArray("result");
                    // 通过for循环解析JSON数组内的数据
                    for (int i = 0; i < jsonArray.length(); i++) {
                        // 创建JSON对象
                        JSONObject result = jsonArray.optJSONObject(i);
                        // 解析对象中的内容
                        int id = result.optInt("id", 0);
                        String question = result.optString("question", null);
                        int answer = result.optInt("answer", 0);
                        String item1 = result.optString("item1", null);
                        String item2 = result.optString("item2", null);
                        String item3 = result.optString("item3", null);
                        String item4 = result.optString("item4", null);
                        String explains = result.optString("explains", null);
                        String url = result.optString("url", null);
                        Log.d(TAG, "解析到的JSON数据 -> id: " + id + ", question: " + question + ", answer: " + answer + ", item1: " + item1 + ", item2: " + item2 + ", item3: " + item3 + ", item4: " + item4 + ", explains: " + explains + ", url: " + url);
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                // 错误思路
//                QuestionBean bean = response.body();
//                JSONObject object = JSONObject.fromObject(bean);
//                JSONArray array = JSONArray.fromObject(bean);
//                String strJSON = object.toString();
//                String strArray = array.toString();
//                System.out.println(strJSON);
//                System.out.println(strArray);
            }
            @Override
            public void onFailure(Call<QuestionBean> call, Throwable t) {
                Log.d(TAG, FAILURE);
            }
        });
    }

}